import {
	req
} from '../../common/vmeitime-http/index.js';
import api from '../../common/api.js';

// state
const state = {
	userInfo: {},
	token: '',
};
// mutations
const mutations = {
	SET_TOKEN(state, token) {
		uni.setStorageSync('token', token);
		state.token = token;
		// console.log('setToken', token);
	},
	SET_USERINFO(state, userInfo) {
		uni.setStorageSync('userInfo', userInfo);
		state.userInfo = userInfo;
	},
	LOGIN_OUT(state, userInfo) {
		uni.removeStorageSync('userInfo');
		uni.removeStorageSync('token');
		state.userInfo = {};
		state.token = '';
	},
};
// actions
const actions = {
	getInfo({
		commit
	}, data) {
		return new Promise((resolve, reject) => {
			req('POST', api.getUserInfo, {
				id: data
			}).then((res) => {
				console.log(res, '更新用户信息');
				// commit('SET_TOKEN', res.data.openid);
				commit('SET_USERINFO', res.data);
				resolve('')
				reject('')
			})
		});
	},
	setToken({
		commit
	}, data) {
		commit('SET_TOKEN', data)
	},
	loginOut({
		commit
	}, data) {
		commit('LOGIN_OUT', data);
	},
	upDataUserInfo({
		commit
	}, dataT) {
		commit('SET_USERINFO', dataT);
	}
};

export default {
	namespaced: true,
	state,
	mutations,
	actions
};